(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.trustedSrc.createVerifyController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'trustedSrcService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, trustedSrcService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.trustedSrc = {};

		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.trustedSrcSites.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.trustedSrc.trustedSrcSites';
			ctrl.innerGridOptions.verifyData = 'vm.trustedSrcOld.trustedSrcSites';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			trustedSrcService.initDataOptions(ctrl);
			authService.createVerify(params).then(function(response) {
				ctrl.trustedSrc = response.data.model || {};
				ctrl.trustedSrc.taskId = response.data.task.taskId;
			});
		};


		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRowsSite = selectedRows;
		}
		
		ctrl.createVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.trustedSrc.comments = comments;
						authService.createVerifyReject(ctrl.trustedSrc).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.createVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.trustedSrc.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createVerifyApprove(ctrl.trustedSrc).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.trustedsrc.list',
					'state' : 'cda.trustedSrc.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.trustedsrc.list',
					'state' : 'cda.trustedSrc.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);